﻿using AutoMapper;
using MediatR;
using Production_Set.API.Application.Commands.Customerser;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Customers;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Orderers
{
    /// <summary>
    /// 订单行修改
    /// </summary>
    public class OrderLineUpdateHandler : IRequestHandler<OrderLineUpdateCommend, Result>
    {
        private readonly IBaseRepository<OrderLine> OrderLine;
        private readonly IMapper mapper;

        public OrderLineUpdateHandler(IBaseRepository<OrderLine> OrderLine, IMapper mapper)
        {
            this.OrderLine = OrderLine;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(OrderLineUpdateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<OrderLine>(request);
            string sql = "UPDATE OrderLine SET InventoryInfoId=@InventoryInfoId ,Prices=@Prices ,Nums=@Nums ,Unit=@Unit " +
                         ",Summary=@Summary WHERE OrderLineId =@OrderLineId";
            var list = OrderLine.Update(sql, new
            {
                OrderLineId = AddData.OrderLineId,
                InventoryInfoId = AddData.InventoryInfoId,
                Prices = AddData.Prices,
                Nums = AddData.Nums,
                Unit = AddData.Unit,
                Summary = AddData.Summary,

            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }

            return results;
        }
    }
}
